package myPetStore.web.servlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.xdevapi.JsonString;
import myPetStore.domain.Cart;
import myPetStore.domain.CartItem;
import myPetStore.domain.Item;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

public class ChangeMyCartServlet extends HttpServlet {


    private Cart cart;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从对话中，获取购物车
        HttpSession session = request.getSession();
        cart = (Cart)session.getAttribute("cart");
        List<String> itemsId = new ArrayList<>();
        List<String>itemsQuantity=new ArrayList<>();

//        List<String> itemsID = JSON.parseArray(request.getParameter("allItemNumber"),List<String>.class);
        String jsonStr1 = request.getParameter("itemsId");
        String jsonStr2 = request.getParameter("itemsQuantity");

        //将前端得到的数组转换为String[]数组
        getJSONArray(jsonStr1,itemsId,jsonStr2,itemsQuantity);

//        System.out.println(request.getAttribute("itemsNumber"));
//        System.out.println(request.getAttribute("itemsPrice"));
        Iterator<CartItem> cartItemIterator = cart.getAllCartItems();

        int i=0;
        BigDecimal [] itemTotal =new BigDecimal[itemsId.size()+1];
        while (cartItemIterator.hasNext()) {
            cartItemIterator.next();
            if(Integer.parseInt(itemsQuantity.get(i)) < 1 ) {
                cart.removeItemById(itemsId.get(i));
            }
            cart.setQuantityByItemId(itemsId.get(i), Integer.parseInt(itemsQuantity.get(i)));

            i++;
        }

        List<CartItem>list =cart.getCartItemList();

//        for(CartItem item :list) {
//            itemTotal[i] = item.getTotal();
////            cartTotal.add(item.getTotal());
//            System.out.println(item.getTotal());
//            i++;
//        }
        for(i=0;i<itemsId.size();i++){
            CartItem item = list.get(i);
            itemTotal[i] = item.getTotal();
        }
        itemTotal[i]=cart.getSubTotal();

        session.setAttribute("cart", cart);


//        Iterator<CartItem> cartItemIterator2 = cart.getAllCartItems();
//        String quantityAll = "";
//        while (cartItemIterator2.hasNext()) {
//            //产品数量增加
//            CartItem cartItem2 = cartItemIterator2.next();
//            int quantity2 = cartItem2.getQuantity();
//            quantityAll += quantity2 + "," + cartItem2.getTotal() + "," + cart.getSubTotal();
//        }
        response.setContentType("text/json");
        String jsonString = JSON.toJSONString(itemTotal);
        PrintWriter out = response.getWriter();
//        out.write(quantityAll);
        out.println(jsonString);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    public static void getJSONArray(String jsonString1 , List<String> list1 , String jsonString2 , List<String> list2 ){
        String temp1=jsonString1.substring(2,jsonString1.length()-2);
        String temp2=jsonString2.substring(2,jsonString2.length()-2);
        String[] array1=temp1.split("\"");
        String[] array2=temp2.split("\"");
        for (String s : array1) {
            if (!Objects.equals(s, ",")) {
                list1.add(s);
            }
        }
        for (String s : array2) {
            if (!Objects.equals(s, ",")) {
                list2.add(s);
            }
        }
    }



}
